package com.ctvit.breadking.news.core.utils;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间工具
 *
 * @author hl
 */
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {

    private DateUtils() {
        throw new IllegalStateException("Utility class");
    }

    public static final String PATTERN_YMD = "yyyy-MM-dd";
    public static final String PATTERN_YMDHM0 = "yyyy-MM-dd HH:mm:00";
    public static final String PATTERN_YMDHMS = "yyyy-MM-dd HH:mm:ss";

    public static final DateTimeFormatter FORMATTER_YMD = DateTimeFormatter.ofPattern(PATTERN_YMD);
    public static final DateTimeFormatter FORMATTER_YMDHM0 = DateTimeFormatter.ofPattern(PATTERN_YMDHM0);
    public static final DateTimeFormatter FORMATTER_YMDHMS = DateTimeFormatter.ofPattern(PATTERN_YMDHMS);

    public static String format(final Date date, final DateTimeFormatter formatter) {
        return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).format(formatter);
    }

    public static String format() {
        return formatYmdHms();
    }

    public static String format(final Date date) {
        return formatYmdHms(date);
    }

    public static String formatYmd() {
        return format(new Date(), FORMATTER_YMD);
    }

    public static String formatYmdHm0() {
        return format(new Date(), FORMATTER_YMDHM0);
    }

    public static String formatYmdHms() {
        return format(new Date(), FORMATTER_YMDHMS);
    }

    public static String formatYmd(final Date date) {
        return format(date, FORMATTER_YMD);
    }

    public static String formatYmdHm0(final Date date) {
        return format(date, FORMATTER_YMDHM0);
    }

    public static String formatYmdHms(final Date date) {
        return format(date, FORMATTER_YMDHMS);
    }

    public static Date parse(final String text, final DateTimeFormatter formatter) {
        return Date.from(LocalDateTime.parse(text, formatter).atZone(ZoneId.systemDefault()).toInstant());
    }

    public static Date parse(final String text) {
        return parseYmdHms(text);
    }

    public static Date parseYmd(final String text) {
        return Date.from(LocalDate.parse(text, FORMATTER_YMD).atStartOfDay(ZoneId.systemDefault()).toInstant());
    }

    public static Date parseYmdHm0(final String text) {
        return Date.from(LocalDateTime.parse(text, FORMATTER_YMDHMS).withSecond(0).atZone(ZoneId.systemDefault()).toInstant());
    }

    public static Date parseYmdHms(final String text) {
        return parse(text, FORMATTER_YMDHMS);
    }

    public static Date getDate() {
        return new Date();
    }

    public static LocalDateTime getLocalDateTime() {
        return LocalDateTime.now();
    }

    public static long getTime(LocalDateTime localDateTime) {
        return localDateTime.toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
    }

    public static String getOneWeekBefore() {
        LocalDateTime minus = LocalDateTime.now().plusDays(-7);
        return minus.format(FORMATTER_YMDHMS);
    }

    public static Date obtainUtcDate(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.HOUR, -8);
        return calendar.getTime();
    }

    public static Date localDateTimeToDate(LocalDateTime localDateTime) {
        // 获取系统默认时区
        ZoneId systemDefault = ZoneId.systemDefault();
        // 将 LocalDateTime 转换为 ZonedDateTime
        ZonedDateTime zonedDateTime = localDateTime.atZone(systemDefault);
        // 将 ZonedDateTime 转换为 Instant
        Instant instant = zonedDateTime.toInstant();
        // 使用 Instant 来创建一个 Date 对象
        return Date.from(instant);
    }

}
